import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:flutter_svg/svg.dart';
import 'package:get/get.dart';

import '../generated/assets.dart';
import '../generated/l10n.dart';
import '../res/colors.dart';
import '../res/text_style.dart';

class CommonWidget {
  static Widget placeHolderImage({
    double width = 80,
    double height = 80,
  }) {
    return Image.asset(
      "assets/health_images/user/head.png",
      width: width.w,
      height: height.w,
    );
  }

  static BoxDecoration getCardStyleCorner(int listLength, int index,
      {Color color = ColorStyle.white}) {
    BorderRadius borderRadius;

    if (listLength == 1) {
      borderRadius = BorderRadius.all(Radius.circular(20.w));
    } else {
      if (index == 0) {
        borderRadius = BorderRadius.only(
            topLeft: Radius.circular(20.w), topRight: Radius.circular(20.w));
      } else if (index == listLength - 1) {
        borderRadius = BorderRadius.only(
            bottomLeft: Radius.circular(20.w),
            bottomRight: Radius.circular(20.w));
      } else {
        borderRadius = BorderRadius.zero;
      }
    }

    return BoxDecoration(
      color: color,
      borderRadius: borderRadius,
    );
  }

  // static CustomFooter buildCustomFooter() {
  //   return CustomFooter(
  //     builder: (BuildContext context, LoadStatus? mode) {
  //       Widget body;
  //       if (mode == LoadStatus.idle) {
  //         // 空闲状态
  //         body = const Text('上拉加载更多');
  //       } else if (mode == LoadStatus.loading) {
  //         // 正在加载状态
  //         body = const Row(
  //           mainAxisAlignment: MainAxisAlignment.center,
  //           children: [
  //             SizedBox(
  //               width: 16,
  //               height: 16,
  //               child: CircularProgressIndicator(
  //                 strokeWidth: 2.0,
  //               ),
  //             ),
  //             SizedBox(width: 12),
  //             Text('加载中...'),
  //           ],
  //         );
  //       } else if (mode == LoadStatus.failed) {
  //         // 加载失败状态
  //         body = const Text('加载失败，请重试');
  //       } else if (mode == LoadStatus.canLoading) {
  //         // 可以加载状态
  //         body = const Text('松开立即加载更多');
  //       } else {
  //         // 没有更多数据状态
  //         body = const Text('没有更多数据');
  //       }
  //
  //       return SizedBox(
  //         height: 55.0,
  //         child: Center(child: body),
  //       );
  //     },
  //   );
  // }

  static Widget commonBtn(String title, VoidCallback callback,
      {double width = 120,
      double height = 28,
      Color bgColor = const Color(0xFF1C2081),
      int alpha = 255,
      Color textColor = Colors.white,
      double textSize = 14,
      FontWeight textFontWeight = FontWeight.w400}) {
    return SizedBox(
      height: height,
      width: width,
      child: ElevatedButton(
        onPressed: () {
          callback();
        },
        style: ButtonStyle(
          backgroundColor: WidgetStateProperty.resolveWith<Color>((states) {
            if (states.contains(WidgetState.disabled)) {
              return bgColor.withAlpha(alpha);
            }
            return bgColor;
          }),
          elevation: WidgetStateProperty.all(0),
          shape: WidgetStateProperty.all(RoundedRectangleBorder(
            // 边框圆角
            borderRadius: BorderRadius.circular(5.r),
          )),
        ),
        child: Text(title,
            style: TextStyle(
                color: textColor,
                fontSize: textSize.sp,
                fontWeight: textFontWeight)),
      ),
    );
  }

  static Widget commonBtn2(String title, VoidCallback callback,
      {double width = 300,
      double height = 50,
      bool state = true,
      Color bgColor = const Color(0xff1C2081),
      Color bgColorUnclickable = const Color(0xffEBEBEB),
      double textSize = 16,
      bool customTitleColor = false,
      Color titleColor = Colors.white,
      FontWeight textFontWeight = FontWeight.w500}) {
    return Container(
      alignment: Alignment.center,
      padding: EdgeInsets.only(left: 32.w, right: 32.w),
      child: SizedBox(
        height: height,
        width: width,
        child: ElevatedButton(
          onPressed: state
              ? () {
                  callback();
                }
              : null,
          style: ButtonStyle(
            backgroundColor: WidgetStateProperty.resolveWith<Color>((states) {
              if (states.contains(WidgetState.disabled)) {
                return bgColorUnclickable;
              }
              return bgColor;
            }),
            elevation: WidgetStateProperty.all(0),
            shape: WidgetStateProperty.all(RoundedRectangleBorder(
              // 边框圆角
              borderRadius: BorderRadius.circular(height / 2),
            )),
          ),
          child: Text(title,
              style: TextStyle(
                  color: customTitleColor
                      ? titleColor
                      : (state ? Colors.white : const Color(0xFF666666)),
                  fontSize: textSize,
                  fontWeight: textFontWeight)),
        ),
      ),
    );
  }

  /// 线--横向
  static Widget lineHorizontal() {
    return Container(
      margin: EdgeInsets.only(top: 15.h, bottom: 20.h),
      width: double.infinity,
      height: 0.5.h,
      color: ColorStyle.color818181,
    );
  }

  /// 带投影Container
  static Widget shadowContainer(Widget childWidget,
      {double top = 12,
      double bottom = 12,
      double left = 12,
      double right = 12,
      double padding = 12}) {
    return Container(
      margin: EdgeInsets.only(
          top: top.h, bottom: bottom.h, left: left.w, right: right.w),
      padding: EdgeInsets.all(padding),
      width: double.infinity,
      decoration: BoxDecoration(
        borderRadius: const BorderRadius.all(Radius.circular(10)),
        boxShadow: [
          BoxShadow(
            color: Colors.grey.withOpacity(0.2), // 设置投影颜色和透明度
            spreadRadius: 0.1, // 控制投影的扩散程度
            blurRadius: 2, // 控制投影的模糊程度
            offset: const Offset(0, 0.5), // 控制投影的偏移量
          ),
        ],
      ),
      child: childWidget,
    );
  }

  /// 暂无数据
  static Widget noData(
      {String assetSvg = Assets.svgNodata,
      double assetWidth = 246,
      double assetHeight = 175,
      double? topSpace,
      String text = ""}) {
    return Column(
      mainAxisAlignment: (topSpace ?? 0) == 0
          ? MainAxisAlignment.center
          : MainAxisAlignment.start,
      children: [
        Container(
          margin: EdgeInsets.only(top: topSpace ?? 0),
          child: SvgPicture.asset(
            assetSvg,
            width: assetWidth.w,
            height: assetHeight.h,
            fit: BoxFit.cover,
          ),
        ),
        Container(
          margin: EdgeInsets.only(top: 7.h),
          width: assetWidth.w,
          child: Text(
            textAlign: TextAlign.center,
            text.isEmpty ? S.current.noData : text,
            style: TextStyles.textStyle(const Color(0xFF979797), 14.sp),
          ),
        ),
      ],
    );
  }

  /// listVew 的BoxDecoration
  static BoxDecoration listVewBoxDecoration(int allLength, int index,
      {Color color = Colors.white}) {
    BoxDecoration boxDecoration = BoxDecoration(
      color: color,
      borderRadius: const BorderRadius.only(
        topLeft: Radius.circular(20),
        topRight: Radius.circular(20),
      ),
    );

    if (allLength == 1) {
      /// 只有1个
      boxDecoration = BoxDecoration(
        color: color,
        borderRadius: const BorderRadius.all(Radius.circular(20)),
      );
    } else if (index == 0) {
      /// 顶部
      boxDecoration = BoxDecoration(
        color: color,
        borderRadius: const BorderRadius.only(
          topLeft: Radius.circular(20),
          topRight: Radius.circular(20),
        ),
      );
    } else if (index == 0) {
      /// 底部
      boxDecoration = const BoxDecoration(
        color: Colors.white,
        borderRadius: BorderRadius.only(
          bottomLeft: Radius.circular(20),
          bottomRight: Radius.circular(20),
        ),
      );
    } else {
      /// 中间
      boxDecoration = const BoxDecoration(
        color: Colors.white,
      );
    }

    return boxDecoration;
  }

  static Widget button(String title, bool isBtnEnabled, VoidCallback callBack) {
    return GestureDetector(
      onTap: () {
        if (isBtnEnabled) callBack();
      },
      child: Container(
        alignment: Alignment.center,
        height: 44.h,
        decoration: BoxDecoration(
          color: isBtnEnabled
              ? ColorStyle.color_ff1877ff
              : ColorStyle.color_ff1877ff.withOpacity(0.6),
          borderRadius: BorderRadius.circular(8.r),
        ),
        child: Text(
          title,
          style: TextStyle(
              color: isBtnEnabled
                  ? Colors.white
                  : ColorStyle.white.withOpacity(0.8),
              fontWeight: FontWeight.w600,
              fontSize: 16.sp),
        ),
      ),
    );
  }

  static AppBar appBar(String title,
      {Widget? backButton,
      List<Widget>? actions,
      Color? backgroundColor,
      bool showBack = true}) {
    return AppBar(
      scrolledUnderElevation: 0.0,
      centerTitle: true,
      title: Text(
        title,
        style: TextStyle(
          fontSize: 16.0.sp,
          color: const Color(0xff333333),
          fontWeight: FontWeight.w500,
          fontFamily: "PingFang SC-Medium",
        ),
      ),
      leading: showBack
          ? backButton ??
              IconButton(
                onPressed: () {
                  Get.back();
                },
                icon: SvgPicture.asset(
                  Assets.svgBack,
                  width: 13.w,
                  height: 20.h,
                ),
                // icon: Image.asset(Assets.svgBack)
              )
          : null,
      backgroundColor: backgroundColor ?? Colors.white,
      elevation: 0.0,
      actions: actions,
    );
  }

  static Widget line() {
    return Divider(color: Colors.white.withOpacity(0.2), height: 0.5);
  }

  Widget nextWidget({double? size}) => SvgPicture.asset(
        Assets.svgArrowRight,
        width: size ?? 12.w,
        height: size ?? 12.h,
      );

  static BoxDecoration containerBg({double? opacity}) {
    return BoxDecoration(
        borderRadius: BorderRadius.circular(5.r),
        color: Colors.white.withOpacity(opacity ?? 0.1));
  }
}
